package com.luli.controllor;

import com.luli.Utils.RedisUtils;
import com.luli.entity.UserData.UpdateUserParam;
import com.luli.entity.login.RegisterUser;
import com.luli.entity.response.ResponseMsg;
import com.luli.entity.Users;
import com.luli.service.RedisUsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.luli.Utils.ResponseMsgUtil.getFailMsg;
import static com.luli.Utils.ResponseMsgUtil.getSuccessMsg;

/**
 * 陆离
 * 2022/5/5 13:30:42
 */
@RestController
@RequestMapping("/redisPojo")
@Slf4j
@CrossOrigin
public class RedisController {
    @Autowired
    private RedisUsersService usersService;
    @Autowired
    private RedisUtils redisUtil;

    @GetMapping("/redisPing")
    public Map<String, Object> redisPing() {
        Boolean aBoolean = redisUtil.linkStatus("192.168.175.135", 6379,"qwe123");
        System.out.println("--------------" + aBoolean);
        Boolean aBoolean2 = redisUtil.linkStatus("172.16.201.60", 6379);
        System.out.println("--------------" + aBoolean2);
        Map<String, Object> map = new HashMap<>();
        map.put("aBoolean",aBoolean);
        map.put("aBoolean2",aBoolean2);
        return map;
    }

    /**
     * redis启动后测试请求
     *
     * @return
     */
    @GetMapping("/createTest")
    public Object test1() {
        redisUtil.set("createTest", "测试", 10, TimeUnit.HOURS);
        redisUtil.expire("y", 10);
        Object y = redisUtil.get("createTest");
        log.info("redis查询数据过期时间:{}", redisUtil.getExpire("createTest"));
        log.info("test:{}", y);
        return y;
    }


    /**
     * redis获取数据库数据库数据存入redis并设置过期时间为10s
     *
     * @param name
     * @return
     */
    @GetMapping("/redisGetUsername")
    public ResponseMsg<?> redisGetUsername(String name) {
        try {
            List<Users> users = usersService.getUsers(name);
            if (users != null) {
                redisUtil.set("usersData", String.valueOf(users), 10,TimeUnit.SECONDS);
            }
            return getSuccessMsg(null, "查询成功！", users);
        } catch (Exception e) {
            return getFailMsg(null, "查询失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/RediaAddData", method = RequestMethod.POST)
    public ResponseMsg<?> RediaAddData(@RequestBody RegisterUser listData) {
        try {
            return usersService.addData(listData);
        } catch (Exception e) {
            return getFailMsg(null, "新增失败！", e.getMessage());
        }
    }


    @RequestMapping(value = "/RediaUpdateData", method = RequestMethod.PUT)
    public ResponseMsg<?> RediaUpdateData(@RequestBody UpdateUserParam listData) {
        try {
            return usersService.updateData(listData);
        } catch (Exception e) {
            return getFailMsg(null, "修改失败！", e.getMessage());
        }
    }


    @RequestMapping(value = "/RediaDelData", method = RequestMethod.DELETE)
    public ResponseMsg<?> RediaDelData(@RequestBody String account) {
        try {
            return usersService.delData(account);
        } catch (Exception e) {
            return getFailMsg(null, "删除失败！", e.getMessage());
        }
    }
}
